package com.ai.dtcore.util;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;

/**
 * SPRING PROERTIES 解析类
 */
public class PropertyConfigurer extends
PropertyPlaceholderConfigurer {

    private static Map<String, String> ctxPropertiesMap = new HashMap<String, String>();


    @Override
    protected void loadProperties(Properties props) throws IOException {
        super.loadProperties(props);
        //默认读取每个属性文件，反向解析密码
        try {
            String password = props.getProperty("password");
            String decryPassword = new String(EncryptionUtil.decode(
                EncryptionUtil.hex2byte(password), "asiainfo".getBytes()));
            props.setProperty("password", decryPassword);
        } catch (Exception e) {
            logger.error("decode password in properties error!", e);
        }
    }



    @Override
    protected void processProperties(
        ConfigurableListableBeanFactory beanFactoryToProcess,
        Properties props) throws BeansException {
        super.processProperties(beanFactoryToProcess, props);
        for (Object key : props.keySet()) {
            String keyStr = key.toString();
            String value = props.getProperty(keyStr);
            ctxPropertiesMap.put(keyStr, value);
        }
    }

    public static String getContextProperty(String name) {
        return ctxPropertiesMap.get(name);
    }

}